﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;

using Model;

namespace DAL
{
    public class AdminUserDAL
    {
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static  DataSet GetListByName(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ExamDB.dbo.tb_EnterpriseUsers");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where users_name='" + strWhere+"'");
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetListById(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ExamDB.dbo.tb_EnterpriseUsers");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where users_id=" + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public static bool CheckLogin(AdminUser adminUser)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select count(1) from ExamDB.dbo.tb_AdminUsers");

            strSql.Append(" where users_name=@userName and users_password=@userPassword");

            SqlParameter[] parameters = {new SqlParameter("@userName", SqlDbType.VarChar,255),
                                            new SqlParameter("@userPassword",SqlDbType.VarChar,255)};

            parameters[0].Value = adminUser.UserNm ;
            parameters[1].Value = Encrypt(adminUser.UserPw);
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public static int UpdatePassword(string userName, string passWord)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_AdminUsers set ");
            strSql.Append("users_password=").Append("'").Append(Encrypt(passWord)).Append("'").
                Append(" where users_name=").Append("'").Append(userName).Append("'");

            int nRows = DbHelperSQL.ExecuteSql(strSql.ToString());
            return nRows;
        }

        public static bool Exists(int useId)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select count(1) from ExamDB.dbo.tb_AdminUsers");

            strSql.Append(" where users_id=@userId ");

            SqlParameter[] parameters = {new SqlParameter("@userId", SqlDbType.Int,4)};

            parameters[0].Value = useId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 用户加密函数，对于用户密码进行MD5加密
        /// </summary>
        public static String Encrypt(string password)
        {
            Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
            Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

            return BitConverter.ToString(hashedBytes);
        }
    }
}
